﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphTools
{
    public class Node : INode
    {
        protected static int nextID = 0;

        public int Id { get; protected set; }
        public float Latitude { get; protected set; }
        public float Longitude { get; protected set; }

        protected static int GetNextId()
        {
            return nextID++;
        }

        public Node(float lat, float lon) : this(GetNextId(), lat,lon)
        {
        }

        public Node(int Id, float lat, float lon)
        {
            this.Id = Id;
            this.Latitude = lat;
            this.Longitude = lon;
        }

        protected static double DegreeToRadian(double angle)
        {
           return Math.PI * angle / 180.0;
        }

        public static double GetDistance(Node n1, Node n2)
        {
            return Haversine.Distance(n1, n2, Haversine.DistanceType.Kilometers);
        }

        public static Node CalculateAverage(ICollection<Node> nodes)
        {
            float lat=0, lon=0;
            foreach (Node n in nodes)
            {
                lat += n.Latitude;
                lon += n.Longitude;
            }
            return new Node(lat / nodes.Count, lon / nodes.Count);
        }

        public override string ToString()
        {
            return "ID: "+Id+" lat: " + Latitude + " lon: " + Longitude;
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return GetHashCode().Equals(obj.GetHashCode());
        }
    }
}
